package com.quexin.pickmedialib

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import kotlinx.android.synthetic.main.activity_picker_media.*

class PickerMediaActivity : Activity() {

    private lateinit var title: String

    private lateinit var pickerMediaParameter: PickerMediaParameter
    private lateinit var adapter: PickerMediaAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_picker_media)
        init()
    }

    private fun init() {
        topBar.addLeftBackImageButton().setOnClickListener { finish() }
        topBar.addRightTextButton("确定", R.id.qmui_dialog_edit_right_icon)
                .setOnClickListener { nextStep() }

        pickerMediaParameter = intent.getParcelableExtra(PickerMediaParameter.PICKER_MEDIA)
                ?: PickerMediaParameter()

        title = when (pickerMediaParameter.type) {
            PickerMediaParameter.TYPE_PICTURE -> "图片"
            PickerMediaParameter.TYPE_VIDEO -> "视频"
            else -> "音频"
        }
        topBar.setTitle("选择$title")

        val pickerData =
                intent.getParcelableArrayListExtra<MediaModel>(PickerMediaParameter.PICKER_MEDIA_DATA)
        adapter = if (pickerData != null && pickerData.isNotEmpty()) {
            PickerMediaAdapter(arrayListOf(), pickerMediaParameter.max, pickerData)
        } else {
            PickerMediaAdapter(arrayListOf(), pickerMediaParameter.max)
        }
        adapter.setListener {
            val unit = if (pickerMediaParameter.type == PickerMediaParameter.TYPE_PICTURE) "张" else "个"
            Toast.makeText(this, "最多${pickerMediaParameter.max}$unit$title", Toast.LENGTH_SHORT).show()
        }
        recycler_picker_media.layoutManager = GridLayoutManager(this, 4)
        recycler_picker_media.adapter = adapter
        (recycler_picker_media.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false

        loadData()
    }

    private fun loadData() {
        when (pickerMediaParameter.type) {
            PickerMediaParameter.TYPE_PICTURE -> loadPic()
            PickerMediaParameter.TYPE_VIDEO -> loadVideo()
            else -> loadAudio()
        }
    }

    private fun loadPic() {
        MediaUtils.loadPictures(this) {
            adapter.setNewInstance(it)
            isEmpty()
        }
    }

    private fun loadVideo() {
        MediaUtils.loadVideos(this) {
            adapter.setNewInstance(it)
            isEmpty()
        }
    }

    private fun loadAudio() {
        MediaUtils.loadAudios(this) {
            adapter.setNewInstance(it)
            isEmpty()
        }
    }

    private fun isEmpty() {
        if (adapter.itemCount > 0) {
            empty_picker_media.hide()
            recycler_picker_media.visibility = View.VISIBLE
        } else {
            empty_picker_media.show(
                    false, "暂无$title",
                    null, null, null
            )
        }
    }

    private fun nextStep() {
        if (adapter.checkModels.isEmpty()) {
            Toast.makeText(this, "请选择$title", Toast.LENGTH_SHORT).show()
            return
        }
        if (adapter.checkModels.size < pickerMediaParameter.min) {
            val unit = if (pickerMediaParameter.type == PickerMediaParameter.TYPE_PICTURE) "张" else "个"
            Toast.makeText(this, "最少${pickerMediaParameter.min}$unit$title", Toast.LENGTH_SHORT).show()
            return
        }
        val pickerIntent = Intent()
        pickerIntent.putExtra(PickerMediaParameter.PICKER_MEDIA, adapter.checkModels)
        setResult(PickerMediaParameter.RESULT_OK, pickerIntent)
        finish()
    }
}